package group.anglers.om.core.persistence.local;

import group.anglers.om.core.exceptions.OmException;
import group.anglers.om.core.exceptions.UnExpectedException;
import group.anglers.om.core.persistence.impls.PersistenceImpl;
import group.anglers.om.core.persistence.serialization.impl.LocalModelSerializationArchived;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 
 * @author an.pham
 *
 */
public class LocalPersistence extends PersistenceImpl {
	private Connection connection;
	
	public LocalPersistence() throws OmException {
		super(LocalModelSerializationArchived.getInstance());
		
		try {
			Class.forName("org.h2.Driver");
			connection = DriverManager.getConnection("jdbc:h2:~/testdb");
			
			startDummy();
			
		} catch (Exception e) {
			throw new UnExpectedException(e);
		}
	}

	@Override
	protected Connection getConnection() {
		return connection;
	}
	
	private void startDummy() throws Exception {
		InputStream inputStream = getClass().getClassLoader().getResourceAsStream("dummy/sql/script.sql");
		
		executeBatch(getClass().getClassLoader().getResourceAsStream("dummy/sql/script.sql"));
		
		inputStream.close();
	}

}
